<?php

namespace App\Http\Traits;

use App\Jobs\Deploy;
use App\Models\RepoSetting;
use App\Models\User;
use Auth;
use Cache;
use Log;
use Storage;

trait DeployTrait
{
    private $deploy_repo_dir = '/data/deploy/storage/repositories';

    private function deployViaSettingId($setting_id, $branch, $checkout_sha, $include_paths = '')
    {
        /** @var RepoSetting $setting */
        $setting        = RepoSetting::find($setting_id, ['id', 'ssh_host', 'repo_url', 'app_path', 'app_logs_path', 'app_configs_path', 'server_env', 'name', 'repo_id', 'exclude_files']);
        $project        = $this->getProject($setting->repo_id);
        $repo_namespace = $project['path_with_namespace'];
        /** @var User $user */
        $user = Auth::user();
        if ($setting) {
            $params = $setting->toArray();
            unset($setting);
            $params['repo_path']      = $this->deploy_repo_dir . '/' . $repo_namespace;
            $params['work_dir']       = $params['repo_path'];
            $params['repo_namespace'] = $repo_namespace;
            $params['user_name']      = $user->name;
            $params['branch']         = $branch;
            $params['include_paths']  = $include_paths;
            $params['checkout_sha']   = $checkout_sha;

            if (Cache::has('cache_sha:' . $checkout_sha)) {
                Log::info('deploy delay 1s.');
                $this->dispatch((new Deploy($params))->delay(1)->onQueue('deploy'));
            } else {
                Cache::add('cache_sha:' . $checkout_sha, 1, 1);
                Log::info('deploy no delay.');
                $this->dispatch((new Deploy($params))->onQueue('deploy'));
            }

            return '【 ' . $params['repo_namespace'] . ' 】【 ' . $params['server_env'] . ' 】已进入部署队列。<a class="btn btn-info" href="' . url('logs/' . $params['server_env'] . '/' . base64_encode($params['repo_namespace'])) . '">查看日志</a>';
        }
    }

    private function deployViaWebHook($project_id, $repo_namespace, $user_name, $checkout_sha, $ref)
    {
        $refs_head = 'refs/heads/';
        if (strpos($ref, $refs_head) === 0) {
            $ref = substr($ref, strlen($refs_head));
        }
        /** @var RepoSetting $setting 默认取第一条 server_env 为 Development，且默认分支为 $ref 的配置 */
        $setting = RepoSetting::where(['repo_id' => $project_id, 'server_env' => 'Development', 'default_branch' => $ref])->orderBy('id')->first(['id', 'ssh_host', 'repo_url', 'app_path', 'app_logs_path', 'app_configs_path', 'server_env', 'default_branch', 'name', 'repo_id', 'exclude_files']);

        if ($setting) {
            $params = $setting->toArray();
            unset($setting);
            $params['repo_path']      = $this->deploy_repo_dir . '/' . $repo_namespace;
            $params['work_dir']       = $params['repo_path'];
            $params['repo_namespace'] = $repo_namespace;
            $params['user_name']      = $user_name;
            $params['branch']         = $params['default_branch'];
            $params['checkout_sha']   = $checkout_sha;

            $this->dispatch((new Deploy($params))->onQueue('deploy'));
        }
    }
}
